<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="分配菜单"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    >
    </SubmitForm>

  </el-dialog>
</template>

<script>
import { http } from '@/api'
import {handleTree} from "@/utils/tree";
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'role',
      dialogValue: false,
      ruleForm: {
        roleId: undefined,
        menuIds: []
      },
      defaultForm: {
        roleId: undefined,
        menuIds: []
      },
      rules: {
      },
      formList: [
        {
          prop: 'menuIds',
          label: '菜单',
          name: 'selectTree',
          checkStrictly: true,
          multiple: true,
          options: [],
          clearable: true,
          props: {
            children: 'children',
            label: 'name',
            value: 'id'
          },
          colSpan: 24
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      console.log('row',row)
      this.dialogValue = true
      http.menu.list({
      }).then(_res => {
        this.formList[0].options = handleTree(_res.data, 'id', 'parentId', 'children');
        if (row) {
          http.role.getRoleMenus(row.id).then(res => {
            this.ruleForm.menuIds = res.data
            this.ruleForm.roleId = row.id
          })
        } else {
          this.ruleForm = {
            ...this.defaultForm
          }
        }
      })



    },
    onSave() {
      http[this.apiKey].updateRoleMenus({ ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
